use std::fmt;

// 用户信息: name, age, height, active
#[derive(Debug)]
struct User(String, i32, f32, bool);

// 自定义显示
impl fmt::Display for User {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "(")?;
        write!(
            f,
            "name:{}, age:{}, height:{}, active:{}",
            self.0, self.1, self.2, self.3
        )?;
        write!(f, ")")
    }
}

// reverse : 反转元素
fn reverse(pair: (&str, i32)) -> (i32, &str) {
    let (a, b) = pair;
    (b, a)
}

fn main() {
    println!("tuples 元组 ");

    let haifeng = User("HF".to_string(), 22, 1.72, true);
    println!("默认显示 Debug: {:?}", haifeng); // User("HF", 22, 1.72, true)
    println!("自定义显示 Display: {}", haifeng); // (name:HF, age:22, height:1.72, active:true)
    println!(
        // name:HF, age:22, height:1.72, active:true
        "name:{}, age:{}, height:{}, active:{}",
        haifeng.0, haifeng.1, haifeng.2, haifeng.3
    );

    println!("单元素元组: {:?}", ("abc",)); // ("abc",)
    println!("单元素元组: {:?}", ("abc")); // "abc"

    println!("反转: {:?} -> {:?}", ("abc", 23), reverse(("abc", 23)));
}
